<?php
require_once('Exception/MException.php');

class MConfig{

	static private $configFile = 'XML/config.xml';
	static private $menuFile = 'XML/mmenu.xml';
	static public $arCfg = null;
	static public $obCfg = null;
	
	/**
	 * Initiate the config file
	 * 
	 * @param string $url
	 * @return void
	 */
	static function init($url=''){
		if($url) self::$configFile = $url;

		self::checkFile(self::$configFile);

		$se = simplexml_load_file(self::$configFile);
		//print_r($se);
		self::$obCfg = $se;
		self::$arCfg = self::object2array($se);

	}
	
	/**
	 * Check file
	 * 
	 * @param string $url
	 * @return boolean
	 */
	static function checkFile($url = ''){
		//if(!$url)throw new MException(__class__.':File ' . $url . ' Not Found');

		if(file_exists($url)):
		return true;
		else:
		throw new MException(__class__.':File ' . $url . ' does not exists');
		endif;
	}
	
	/**
	 * @param simplexmlelement
	 * @return array
	 */
	static function object2array($object){
		$return = NULL;
   
		if(is_array($object))
		{
			foreach($object as $key => $value)
			$return[$key] = self::object2array($value);
		}
		else
		{
			$var = get_object_vars($object);
			 
			if($var)
			{
				foreach($var as $key => $value)
				$return[$key] = self::object2array($value);
			}
			else
			return strval($object); // strval and everything is fine
		}

		return $return;
	}
	
	static function getMMenu($url=''){
		if($url) self::$menuFile = $url;
		
		$se = simplexml_load_file(self::$menuFile);
		
		return self::object2array($se);
	}
}
?>